package com.example.cyh.bczapp.Code.Activity;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.example.cyh.bczapp.Code.BLL.PlanBLL;
import com.example.cyh.bczapp.Code.BLL.UserBLL;
import com.example.cyh.bczapp.Code.Model.PlanInfEntity;
import com.example.cyh.bczapp.Code.Model.RemenberInfEntity;
import com.example.cyh.bczapp.Code.Model.UserInfEntity;
import com.example.cyh.bczapp.Code.Model.WordGroupInfEntity;
import com.example.cyh.bczapp.Code.fragment.TranslateFragment;
import com.example.cyh.bczapp.Code.fragment.HomeFragment;
import com.example.cyh.bczapp.Code.fragment.FindWordFragment;
import com.example.cyh.bczapp.R;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.view.SimpleDraweeView;


public class MainActivity extends Activity {
    //常用地址
    private final String BaseImageUrl = "http://123.207.86.30/public/pic/";
    private final String BaseUrl = "http://123.207.86.30/public/index.php/db/";

    //底部导航中的4个线性试图
    private LinearLayout mTabfindword;
    private LinearLayout mTabhome;
    private LinearLayout mTabplan;
    private LinearLayout mTabreview;
    //4个  视图碎片类
    private TranslateFragment translateFragment;
    private HomeFragment homeFragment;
    private FindWordFragment findWordFragment;
    //背单词数据
    public PlanInfEntity mainplan;
    public UserInfEntity user;
    public RemenberInfEntity todayremenber;
    public WordGroupInfEntity mainwordGroup;

    public boolean hasplan = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fresco.initialize(MainActivity.this);//初始化侧面栏
        setContentView(R.layout.activity_main);
        user = (UserInfEntity) getIntent().getSerializableExtra("user");
        //实例化碎片对象
        translateFragment = new TranslateFragment();
        findWordFragment = new FindWordFragment();
        homeFragment = new HomeFragment();
        //底部导航构造
        BottomNavigationBar bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_nav);
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);//设置显示模式
        bottomNavigationBar.setActiveColor("#262325").setInActiveColor("#A8D9F5");//设置颜色  选中颜色和  未选中的背景色
        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.ic_action_home, "首页"))
                .addItem(new BottomNavigationItem(R.drawable.ic_action_book, "查单词"))
                .addItem(new BottomNavigationItem(R.drawable.ic_action_find, "翻译小助手"))
                .setFirstSelectedPosition(0)//默认选中第一个
                .initialise();
        //申明底部导航事件
        bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                //获取碎片管理类
                FragmentManager fm = getFragmentManager();
                FragmentTransaction transaction = fm.beginTransaction();//保证原子性
                //给碎片视图参数
                switch (position) {
                    case 2: {
                        transaction.replace(R.id.id_content, translateFragment, "findword");
                        break;
                    }
                    case 1: {
                        transaction.replace(R.id.id_content, findWordFragment, "review");
                        break;
                    }
                    case 0: {
                        transaction.replace(R.id.id_content, homeFragment, "home");
                        break;
                    }
                }
                transaction.commit();
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });
        //设置默认的碎片试图
        setDefaultFragment();
        Loadactivity();
    }

    @Override
    protected void onResume() {
        super.onResume();
        new Thread(new Runnable() {
            @Override
            public void run() {
                UserBLL ub=new UserBLL();
                user=ub.IdGetUser(String.valueOf(user.U_Id));
                PlanBLL pb=new PlanBLL();
                if(user.U_PlanId!=null)
                mainplan=pb.GetPlan(user.U_PlanId);
                else
                    mainplan=null;
            }
        }).start();

    }

    private void setDefaultFragment() {
        FragmentManager fm = getFragmentManager();//碎片管理
        FragmentTransaction transaction = fm.beginTransaction();//增删碎片
        transaction.replace(R.id.id_content, homeFragment);
        transaction.commit();
    }

    private void Loadactivity() {
        //加载头像
        String url = BaseImageUrl + user.U_HeadPic;
        SimpleDraweeView simple = (SimpleDraweeView) findViewById(R.id.head_pic);
        simple.setImageURI(url);
//        DraweeController controller = Fresco.newDraweeControllerBuilder()
//                .setUri(url)//URL地址
//                .setTapToRetryEnabled(true)//开启点击重试
//                .build();//构建
//        simple.setController(controller);
        //用户名
        ((TextView) findViewById(R.id.user_name)).setText(user.U_Name);
        LinearLayout plan = (LinearLayout) findViewById(R.id.nav_plan);
        plan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转计划制定页面
                Intent intent = new Intent(MainActivity.this, ChangePlan.class);
                intent.putExtra("user", MainActivity.this.user);
                startActivity(intent);
            }
        });
        findViewById(R.id.nav_setting).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SettingActivity.class);
                startActivity(intent);
            }
        });
    }

}
